package com.comac.print.component.app.command.qryExe;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.comac.print.component.app.domain.gateway.enums.PtStatusEnum;
import com.comac.print.component.app.dto.PrintRecordDTO;
import com.comac.print.component.app.gatewayimpl.database.mapper.PrintRecordMapper;
import com.comac.print.component.app.page.PageResponse;
import com.comac.print.component.app.qry.PrintRecordQry;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;

@Slf4j
@Component
public class PrintRecordQryExe {

    @Autowired
    private PrintRecordMapper printRecordMapper;


    public PageResponse<PrintRecordDTO> qryPrintRecordList(PrintRecordQry printRecordQry) {
        if(printRecordQry.getPageNum()==0 || printRecordQry.getPageSize()==0){
            return  PageResponse.buildFailure("-1", "分页参数不为空！");
        }
        Page page = new Page(printRecordQry.getPageNum(), printRecordQry.getPageSize());
        log.info("---PrintRecordQryExe.qryPrintRecordList---查询打印信息，入参：{}", JSON.toJSONString(printRecordQry));
        Page<PrintRecordDTO> printRecordPage = printRecordMapper.qryPrintRecordList(page,printRecordQry);
        log.info("---PrintRecordQryExe.qryPrintRecordList---查询打印信息，出参：{}", JSON.toJSONString(printRecordPage));
        List<PrintRecordDTO> printRecordDTOList = printRecordPage.getRecords();
        Page<PrintRecordDTO> pageData = new Page();
        List<PrintRecordDTO> result = putBasicInformation(printRecordDTOList);
        pageData.setRecords(result);
        pageData.setCurrent(printRecordPage.getCurrent());
        pageData.setTotal(printRecordPage.getTotal());
        pageData.setSize(printRecordPage.getSize());
        return PageResponse.successPage(pageData);
    }

    private List<PrintRecordDTO> putBasicInformation(List<PrintRecordDTO> printRecordDTODTOList) {
        //设置FAI
        List<PrintRecordDTO> result = new ArrayList<>(printRecordDTODTOList.size());
        for (PrintRecordDTO printRecordDTO : printRecordDTODTOList) {
            PrintRecordDTO printRecordToDTO = new PrintRecordDTO();
            BeanUtils.copyProperties(printRecordDTO, printRecordToDTO);
            //枚举状态的转换
            //PtStatusEnum ptStatusEnumByCode = PtStatusEnum.getPtStatusEnumByCode(printRecordDTO.getStatus());
            //printRecordToDTO.setStatus(ptStatusEnumByCode.getDescription());
            result.add(printRecordToDTO);
        }
        return result;
    }
}
